﻿using System;
using VIT.DataHelper.LinqHelper.Infrastructure;
using DataAccessLayer;
using System.Linq;
using DataTransferObject;
namespace BusinessLogicLayer
{
    public abstract class BLLBase
    {
        private readonly IUnitOfWork _unitOfWork;
        private string _connectionString;
        private string ConnectionString
        {
            get { return this._connectionString ?? System.Configuration.ConfigurationManager.ConnectionStrings["QlPhongKhamEntities"].ConnectionString; }
            set { this._connectionString = value; }
        }
        protected IDatabaseFactory<QlPhongKhamEntities> DatabaseFactory { get; private set; }
        protected void DisposeCore()
        {
            this._unitOfWork.Dispose();
            //base.DisposeCore();
        }
        protected bool CatchError(Exception ex)
        {
            this.RollBack();
            throw ex;
        }
        protected BLLBase(IUnitOfWork unitOfWork)
        {
            this.DatabaseFactory = unitOfWork.DatabaseFactory as IDatabaseFactory<QlPhongKhamEntities>;
            this._unitOfWork = unitOfWork;
        }
        protected BLLBase(string connectionString)
        {
            if (!string.IsNullOrEmpty(connectionString))
            {
                this.ConnectionString = connectionString;
            }
            this.DatabaseFactory = new DatabaseFactory<QlPhongKhamEntities>(this.ConnectionString);
            this._unitOfWork = new UnitOfWork(this.DatabaseFactory);
        }
        protected BLLBase()
            : this((string)null)
        {
        }
        ~BLLBase()
        {
            this.DisposeCore();
        }
        public IUnitOfWork UnitOfWork
        {
            get { return this._unitOfWork; }
        }
        public void BeginWork()
        {
            this._unitOfWork.BeginTransaction();
        }
        public void SaveChanges()
        {
            this._unitOfWork.Commit();
        }

        public void RollBack()
        {
            this._unitOfWork.RollBackTransaction();
        }

        public enum Order
        {
            /// <summary>
            /// Order Up
            /// </summary>
            Up,

            /// <summary>
            /// Order Down
            /// </summary>
            Down
        }
        //#region Public method
        //public IList<SexDto> GetSexs()
        //{
        //    var sexs = new List<SexDto>
        //    {
        //        new SexDto {Id = true, Name= "Nam"},
        //        new SexDto {Id = false, Name= "Nữ"}
        //    };

        //    return sexs;
        //}
       // #endregion
    }
}
